package top.kiterunner.learning.concurrency_test;

import java.util.ArrayList;
import java.util.List;

/*
volatile关键字只能保证可见性，不能保证原子性
*/

/**
 * AdorynZhao in Tencent-UESTC.
 * Source Code Created on 2017/7/6.
 */
public class ThreadDemo8 {

    public volatile int count = 0;
    public Object lock = new Object();

    public void method() {
        for (int i = 0; i < 10000; i++) {
            synchronized (lock) { // 对比有无synchronized关键字过程
                count++;
            }
        }
    }

    public static void main(String[] args) {
        ThreadDemo8 t8 = new ThreadDemo8();

        List<Thread> threadList = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            threadList.add(new Thread(t8::method, "Thread"+i));
        }

        for (Thread t: threadList) {
            t.start();
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println("Count = " + t8.count);
    }
}